package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IApkFilterDAO;
import org.jsbd.boss.domian.channel.ApkFilter;
import org.jsbd.boss.enums.FilterTypeEnum;
import org.jsbd.boss.service.IApkFilterService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import edu.hziee.common.tcp.cache.CachedHolder;

/**
 * TODO 请加入本类的说明
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class ApkFilterServiceImpl implements IApkFilterService {
    public static final Logger LOGGER = LoggerFactory.getLogger(ApkFilterServiceImpl.class);

    @Resource
    private IApkFilterDAO apkFilterDAO;

    @Resource
    private RedisClient redisClient;

    @Resource
    private CachedHolder ehcache;

    @Override
    public List<ApkFilter> selectAllApkFilterList(ApkFilter apkFilter) {
        String cacheKey = CacheKey.APK_FILTER_LIST + apkFilter.getFilterType();
        List<ApkFilter> apkFilterList = (List<ApkFilter>) ehcache.get(cacheKey);
        if (apkFilterList == null) {
            apkFilterList = (List<ApkFilter>) redisClient.get(cacheKey);
            if (apkFilterList != null && apkFilterList.size() > 0) {
                ehcache.put(cacheKey, apkFilterList);
            } else {
                apkFilterList = apkFilterDAO.selectAllApkFilterList(apkFilter);
                if (apkFilterList != null && apkFilterList.size() > 0) {
                    redisClient.put(cacheKey, apkFilterList, 60);
                }else{
                    redisClient.put(cacheKey, new ArrayList<>(), 60);
                }
            }
        }
        return apkFilterList;
    }

    @Override
    public Integer insertApkFilter(ApkFilter apkFilter) {
        return apkFilterDAO.insertApkFilter(apkFilter);
    }

    @Override
    public Integer updateApkFilter(ApkFilter apkFilter) {
        return apkFilterDAO.updateApkFilter(apkFilter);
    }

    @Override
    public ApkFilter selectApkFilter(ApkFilter apkFilter) {
        return apkFilterDAO.selectApkFilter(apkFilter);
    }

    @Override
    public List<ApkFilter> selectAllList(ApkFilter filter) {

        List<ApkFilter> apkFilterList = apkFilterDAO.selectApkFilterList(filter);
        for (ApkFilter apkFilter : apkFilterList) {
            Integer filterType = apkFilter.getFilterType();
            if (filterType != null && filterType == FilterTypeEnum.USEFUL.getType()) {
                apkFilter.setFilterTypeName(FilterTypeEnum.valueOf(filterType).getName());
            } else {
                apkFilter.setFilterTypeName(FilterTypeEnum.UNUSEFUL.getName());
            }
        }
        return apkFilterList;
    }

    @Override
    public Integer selectCount(ApkFilter apkFilter) {
        return apkFilterDAO.selectCount(apkFilter);
    }

    @Override
    public Integer deleteApkFilter(Integer id) {
        return apkFilterDAO.deleteApkFilter(id);
    }


    @Override
    public Integer deleteApk(ApkFilter apkFilter) {
        return apkFilterDAO.deleteApk(apkFilter);
    }

    @Override
    public Integer insertApk(ApkFilter apkFilter) {
        return apkFilterDAO.insertApk(apkFilter);
    }

    @Override
    public List<ApkFilter> selectApkList(ApkFilter apkFilter) {
        return apkFilterDAO.selectApkList(apkFilter);
    }
}
